﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Practices.Unity.InterceptionExtension;

namespace SpectrumAnalyser.Common.Logs
{
    public class LogCallHandler : ICallHandler
    {
        public int Order { get; set; }

        public LogCallHandler() : this(0) { }

        public LogCallHandler(int order)
        {
            Order = order;
        }

        #region ICallHandler Members

        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            Logger.Log(String.Format("{0}: Started", input.MethodBase.Name));

            var sw = new Stopwatch();
            sw.Start();

            var result = getNext().Invoke(input, getNext);

            sw.Stop();

            Logger.Log(String.Format("{0}: Прошло времени {1} мс", input.MethodBase.Name, sw.ElapsedMilliseconds));

            return result;
        }

        #endregion
    }
}